<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="调整" v-model="visible" align-center width="500px" :z-index="2000"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <el-form-item label="预计发货数量" prop="deliver_num" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input-number v-model="form.deliver_num" controls-position="right" :min="0" :step="1" :precision="0"
          class="w-full" />
      </el-form-item>
      <el-form-item label="预计发货日期" prop="deliver_time" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-date-picker v-model="form.deliver_time" placeholder="日期选择" type="date" value-format="YYYY-MM-DD"
          class="w-full" />
      </el-form-item>
      <el-form-item label="发货计划目的仓" prop="plan_destination_warehouse"
        :rules="[{ required: form.plan_type === 1, message: '不能为空', trigger: 'change' }]">
        <VSelect v-model="form.plan_destination_warehouse" placeholder="请选择" :options="options.destination_warehouse"
          class="w-full" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { nextTick, reactive } from 'vue';
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (row) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  const { dp_id, deliver_num, deliver_time, plan_destination_warehouse, plan_type } = row
  form.value = { dp_id, deliver_num, deliver_time, plan_destination_warehouse, plan_type }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  if (!await formRef.validate()) return
  loadings.create = true
  api.adjust(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>